layui.use(['table', 'admin','element', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var element = layui.element;
    localStorage.setItem("avt","sysSellTable")
    /**
     * 挂售管理
     */
    var SysSell = {
        tableId: "sysSellTable"
    };

    var SysSell1 = {
        tableId: "sysSellTable1"
    };
    var SysSell2 = {
        tableId: "sysSellTable2"
    };

    var SysSell3 = {
        tableId: "sysSellTable3"
    };
    var SysSell4 = {
        tableId: "sysSellTable4"
    };
    /**
     * 弹出添加对话框
     */
    SysSell.openAddDlg = function () {
        func.open({
            title: '添加挂售',
            content: Feng.ctxPath + '/sysSell/add',
            tableId: SysSell.tableId
        });
    };
    /**
     * 点击查询按钮
     */
    SysSell.search = function () {
        var queryData = {};
        queryData['contractType'] = $("#contractType").val();
        queryData['stuats'] = $("#stuats").val();
        queryData['payType'] = $("#payType").val();
        var s = localStorage.getItem("avt").replace("#","");
        table.reload(s, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 导出excel按钮
     */
    SysSell.exportExcel = function () {
        var checkRows = table.checkStatus(SysSell.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };





    // 工具条点击事件
    table.on('tool(' + SysSell.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            chat(data, "sysSellTable");
        } else if (layEvent === 'agree') {
            agree(data,"sysSellTable")
        }else if (layEvent === 'refuse') {
            refuse(data,"sysSellTable")
        }else if (layEvent === 'pass') {
            dismiss(data,"sysSellTable",1)
        }else if (layEvent === 'dismiss') {
            dismiss(data,"sysSellTable",2)
        }else if (layEvent === 'negotiate') {
            dismiss(data,"sysSellTable",3)
        }else if (layEvent === "affirm" ) {
            affirm(data,"sysSellTable")
        }else if (layEvent === "appeal" ) {
            appeal(data,"sysSellTable");
        }else if (layEvent === "unshelve" ) {
            unshelve(data,"sysSellTable");
        }
    });
    // 工具条点击事件
    table.on('tool(' + SysSell1.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            chat(data, "sysSellTable1");
        } else if (layEvent === 'agree') {
            agree(data,"sysSellTable1")
        }else if (layEvent === 'refuse') {
            refuse(data,"sysSellTable1")
        }else if (layEvent === 'pass') {
            dismiss(data,"sysSellTable1",1)
        }else if (layEvent === 'dismiss') {
            dismiss(data,"sysSellTable1",2)
        }else if (layEvent === 'negotiate') {
            dismiss(data,"sysSellTable1",3)
        }else if (layEvent === "affirm" ) {
            affirm(data,"sysSellTable1")
        }else if (layEvent === "appeal" ) {
            appeal(data,"sysSellTable1");
        }else if (layEvent === "unshelve" ) {
            unshelve(data,"sysSellTable1");
        }
    });
    // 工具条点击事件
    table.on('tool(' + SysSell2.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            chat(data, "sysSellTable2");
        } else if (layEvent === 'agree') {
            agree(data,"sysSellTable2")
        }else if (layEvent === 'refuse') {
            refuse(data,"sysSellTable2")
        }
    });
    // 工具条点击事件
    table.on('tool(' + SysSell3.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'edit') {
            chat(data, "sysSellTable3");
        }else if (layEvent === 'pass') {
            dismiss(data,"sysSellTable3",1)
        }else if (layEvent === 'dismiss') {
            dismiss(data,"sysSellTable3",2)
        }else if (layEvent === 'negotiate') {
            dismiss(data,"sysSellTable3",3)
        }
    });


    //触发事件
    var active = {
        tabAdd: function(){
            //新增一个Tab项
            element.tabAdd('demo', {
                title: '新选项'+ (Math.random()*1000|0) //用于演示
                ,content: '内容'+ (Math.random()*1000|0)
                ,id: new Date().getTime() //实际使用一般是规定好的id，这里以时间戳模拟下
            })
        }
        ,tabDelete: function(othis){
            //删除指定Tab项
            element.tabDelete('demo', '44'); //删除：“商品管理”


            othis.addClass('layui-btn-disabled');
        }
        ,tabChange: function(){
            //切换到指定Tab项
            element.tabChange('demo', '22'); //切换到：用户管理
        }
    };

    $('.site-demo-active').on('click', function(){
        var othis = $(this), type = othis.data('type');
        active[type] ? active[type].call(this, othis) : '';
    });

    //Hash地址的定位
    var layid = location.hash.replace(/^#test=/, '');
    element.tabChange('test', layid);

    element.on('tab(test)', function(elem){
        location.hash = 'test='+ $(this).attr('lay-id');
    });


    // 初始化加载
     getList( '#sysSellTable','',"")
    // 监听tab切换 操作：文档 - 内置模块 - 常用元素操作 element - 监听tab切换
    element.on('tab(tab-all)', function (data) {
        // console.log(this);        // 当前Tab标题所在的原始DOM元素
        // console.log(data.index);  // 得到当前Tab的所在下标
        // console.log(data.elem);   // 得到当前的Tab大容器
        var status = $(this).attr('data-status')
        var position = '#sysSellTable'

        switch (status) {
            case '10': position = '#sysSellTable1'; break;
            case '1': position = '#sysSellTable2'; break;
            case '6': position = '#sysSellTable3'; break;
            default: position = '#sysSellTable';
        }
        localStorage.setItem("avt",position)
        if (status=="10"){
            getList(position,"","1")
        } else {
            getList(position,status,"")
        }
    })

    /**
     * 点击查看
     *
     * @param data 点击按钮时候的行数据
     */
    function chat(data,SysSell) {
        console.log(SysSell)
        func.open({
            title: '修改挂售',
            content: Feng.ctxPath + '/sysSell/edit?id=' + data.id,
            tableId: SysSell
        });
    };
    /**
     * 点击同意
     *
     * @param data 点击按钮时候的行数据
     */
    function agree(data,SysSell) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/sysSell/editData", function (data) {
                Feng.success("审批成功!");
                table.reload(SysSell);
            }, function (data) {
                Feng.error("审批失败!" + data.responseJSON.message + "!");
            });
            ajax.set("ids", data.id);
            ajax.set("type", "1");
            ajax.start();
        };
        Feng.confirm("是否同意?", operation);
    };

    /**
     * 点击拒绝
     *
     * @param data 点击按钮时候的行数据
     */
    function refuse(data,SysSell) {
        layer.open({
            type: 1,
            title: '请输入拒绝原因',
            area: ['500px', '300px'],
            btn: ['确定'],
            content: '<div><textarea type="text"  autocomplete="off" class="layui-textarea" lay-verify="required" style="height:200px"></textarea></div>',//这里content是一个普通的String
            yes: function (index, layero) {
                //按钮【按钮一】的回调
                var backreason = layero.find('textarea')[0].value;// 获取输入的退回原因
                console.log(backreason)
                layer.close(index);
                var operation = function () {
                    var ajax = new $ax(Feng.ctxPath + "/sysSell/editData", function (data) {
                        Feng.success("审批成功!");
                        table.reload(SysSell);
                    }, function (data) {
                        Feng.error("审批失败!" + data.responseJSON.message + "!");
                    });
                    ajax.set("ids", data.id);
                    ajax.set("type", "0");
                    ajax.set("remark", backreason);
                    ajax.start();
                };
                Feng.confirm("是否同意?", operation);
            }
        });
    };

    /**
     * 批量拒绝
     *
     * @param data 点击按钮时候的行数据
     */
    function refuseList(backreason) {
        var checkStatus = table.checkStatus(SysSell2.tableId);
        if (checkStatus.data.length == 0) {
            parent.layer.msg('请先选择要删除的数据行！', {icon: 2});
            return;
        }
        var ids = "";
        for (var i = 0; i < checkStatus.data.length; i++) {
            ids += checkStatus.data[i].id + ",";
        }
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/sysSell/editData", function (data) {
                Feng.success("批量审批成功!");
                table.reload(SysSell2.tableId);
            }, function (data) {
                Feng.error("批量审批失败!" + data.responseJSON.message + "!");
            });
            ajax.set("ids", ids);
            ajax.set("type", "0");
            ajax.set("remark", backreason);
            ajax.start();
        };
        Feng.confirm("是否全部拒绝?", operation);

    }

    /**
     * 批量同意
     *
     * @param data 点击按钮时候的行数据
     */
    function agreeList() {
        var checkStatus = table.checkStatus(SysSell2.tableId);
        if (checkStatus.data.length == 0) {
            parent.layer.msg('请先选择要同意的数据行！', {icon: 2});
            return;
        }
        var ids = "";
        for (var i = 0; i < checkStatus.data.length; i++) {
            ids += checkStatus.data[i].id + ",";
        }
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/sysSell/editData", function (data) {
                Feng.success("批量审批成功!");
                table.reload(SysSell2.tableId);
            }, function (data) {
                Feng.error("批量审批失败!" + data.responseJSON.message + "!");
            });
            ajax.set("ids", ids);
            ajax.set("type", "1");
            ajax.start();
        };

        Feng.confirm("是否全部同意?", operation);
    }
    /**
     * 驳回
     *
     * @param data 点击按钮时候的行数据
     */
    function dismiss(data,SysSell,type) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/sysSell/platformApproval", function (data) {
                Feng.success("成功!");
                table.reload(SysSell);
            }, function (data) {
                Feng.error("失败!" + data.responseJSON.message + "!");
            });
            ajax.set("ordinal", data.ordinal);
            ajax.set("type", type);
            ajax.start();
        };
        Feng.confirm("是否同意?", operation);
    };

    /**
     * 平台确认
     *
     * @param data 点击按钮时候的行数据
     */
    function affirm(data,SysSell,type) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/sysSell/appAffirm", function (data) {
                Feng.success("成功!");
                table.reload(SysSell);
            }, function (data) {
                Feng.error("失败!" + data.responseJSON.message + "!");
            });
            ajax.set("ordinal", data.ordinal);
            ajax.set("userid", data.buyerUserid);
            ajax.start();
        };
        Feng.confirm("是否同意?", operation);
    };
    /**
     * 平台确认
     *
     * @param data 点击按钮时候的行数据
     */
    function unshelve(data,SysSell,type) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/sysSell/unshelve", function (data) {
                Feng.success("成功!");
                table.reload(SysSell);
            }, function (data) {
                Feng.error("失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否同意?", operation);
    };
    /**
     * 平台申诉
     *
     * @param data 点击按钮时候的行数据
     */
    function appeal(data,SysSell,type) {

        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/sysSell/refuse", function (data) {
                Feng.success("成功!");
                table.reload(SysSell);
            }, function (data) {
                Feng.error("失败!" + data.responseJSON.message + "!");
            });
            ajax.set("ordinal", data.ordinal);
            ajax.set("userid", data.buyerUserid);
            ajax.start();
        };
        Feng.confirm("是否同意?", operation);
    };





// 公共方法
    function getList(position,stuats,ispt) {

        table.render({
            elem: position
            , method: 'POST'
            , url:Feng.ctxPath + '/sysSell/list'
            , where: {
                stuats: stuats,
                standby1:ispt
            }
            ,page: true
            ,height: "full-158"
            ,cellMinWidth: 100
            , cols:[[
                {type: 'checkbox'},
                {field: 'id', hide: true, title: ''},
                {field: 'buyerUserid', hide: true, title: ''},
                {field: 'ordinal', sort: true, title: '挂售单号'},
                {field: 'price', sort: true, title: '价格',templet: function (d) {
                        return (d.price/100);
                    }},
                {field: 'contractType', sort: true, title: '合约类别',templet: function (d) {
                        var type =d.contractType;
                        if (type == 0){
                            return "<span style='color: DarkSlateGray  '><b>新手合约 </b></span>";
                        } else if (type == 1){
                            return "<span style='color: DarkTurquoise   '><b> 初级合约</b></span>";
                        }else if (type == 2){
                            return "<span style='color: DarkViolet '><b> 中级合约</b></span>";
                        }else if (type ==3){
                            return "<span style='color: Olive   '><b> 高级合约</b></span>";
                        } else {
                            return "<span style='color: red '><b> 参数出现错误请删除重新定义</b></span>";
                        }
                    }},
                {field: 'payType', sort: true, title: '支付方式',templet: function (d) {
                        var type =d.payType;
                        if (type == 0){
                            return "<span style='color: LightSeaGreen'><b>可用余额</b></span>";
                        } else if (type == 1){
                            return "<span style='color: Chocolate '><b>团队返利</b></span>";
                        }else if (type == 2){
                            return "<span style='color: Coral  '><b>推广返利</b></span>";
                        }else if (type ==3){
                            return "<span style='color: fuchsia  '><b>平台挂售</b></span>";
                        } else {
                            return  "<span style='color: red  '><b>参数出现错误请删除重新定义</b></span>";
                        }
                    }},
                {field: 'stuats', sort: true, title: '挂售状态',templet: function (d) {
                        var type =d.stuats;
                        if (type == 0){
                            return "已拒绝"
                        } else if (type == 1){
                            return "<span style='color: red'><b>待审批</b></span>";
                        }else if (type == 2){
                            return "<span style='color: Blue '><b>挂售中</b></span>";
                        }else if (type ==3){
                            return "<span style='color: LightSlateGray '><b>买入，卖出</b></span>";
                        }else if (type ==4){
                            return "<span style='color: Darkorange  '><b>待确认</b></span>";
                        }else if (type ==5){
                            return "<span style='color: Indigo   '><b>完成</b></span>";
                        }else if (type ==6){
                            return "<span style='color: red'><b>申诉中，待解决</b></span>";
                        } else {
                            return "参数出现错误请删除重新定义"
                        }
                    }},
                {field: 'phone', sort: true,width:150, title: '联系方式（申诉）'},
                {field: 'title', sort: true, title: '标题（申诉）'},
                {field: 'createTime', sort: true, title: '挂售时间'},
                {align: 'center', width:350, toolbar: '#tableBar', title: '操作',templet: function (d) {

                        return "参数出现错误请删除重新定义"
                    }
                }
            ]]
        });
    }


    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        SysSell.openAddDlg();
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        SysSell.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
        SysSell.exportExcel();
    });

    // 添加按钮点击事件
    $('#agreeList').click(function () {
        agreeList();
    });
    // 添加按钮点击事件
    $('#refuseList').click(function () {
        layer.open({
            type: 1,
            title: '请输入拒绝原因',
            area: ['500px', '300px'],
            btn: ['确定'],
            content: '<div><textarea type="text"  autocomplete="off" class="layui-textarea" lay-verify="required" style="height:200px"></textarea></div>',//这里content是一个普通的String
            yes: function (index, layero) {
                //按钮【按钮一】的回调
                var backreason = layero.find('textarea')[0].value;// 获取输入的退回原因
                console.log(backreason)
                layer.close(index);
                refuseList(backreason);
            }
        });

    });
});
